package org.example.dao;

import org.example.expection.AppException;
import org.example.model.User;
import org.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

public class LoginDAO {
    //JDBC
    public static User query(String username) {
        Connection c=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            c=DBUtil.getConnection();//从同一个数据库连接池获取
            String sql="select id, username, password, " +
                    "nickname, sex, birthday, head from user " +
                    "where username=?";//创建sql命令对象
            ps=c.prepareStatement(sql);
            //设置占位符
            ps.setString(1,username);
            rs=ps.executeQuery();
            User user=null;
            //处理结果集
            while (rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(username);
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setSex(rs.getBoolean("sex"));
                //关于日期的使用，java中一般使用java.util.Date(年月日时分秒)
                java.sql.Date birthday=rs.getDate("birthday");
                if(birthday!=null)
                    user.setBirthday(new Date(birthday.getTime()));
                user.setHead(rs.getString("head"));
            }
            return user;
        }catch (Exception e){
            throw new AppException("LOG001","查询用户操作出错",e);
        }finally {
            DBUtil.close(c,ps,rs);
        }
    }
}
